<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<title>Insert title here</title>
		<link rel="stylesheet" href="./layui/css/layui.css">
		<script type="text/javascript" src="js/global.js"></script>
		<script src="./layui/layui.js"></script>
	</head>

	<body>
		<div hidden="hidden" class="layui-form-item" style="margin-top: 20px">
			<label class="layui-form-label">ID：</label>
			<div class="layui-input-inline" style="width: 350px">
				<input type="text" name="id" id="id" required lay-verify="required" placeholder="" autocomplete="off" class="layui-input">
			</div>
		</div>
		<div class="layui-form-item" style="margin-top: 20px">
			<label class="layui-form-label">模块名：</label>
			<div class="layui-input-inline" style="width: 350px">
				<input type="text" name="name" id="name" required lay-verify="required" placeholder="" autocomplete="off" class="layui-input">
			</div>
		</div>
		<div hidden="hidden" class="layui-form-item">
			<label class="layui-form-label">父级ID：</label>
			<div class="layui-input-inline" style="width: 350px">
				<input type="text" name="parentId" id="parentId" required lay-verify="required" placeholder="" autocomplete="off" class="layui-input">
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label">父级模块：</label>
			<div class="layui-input-inline" style="width: 350px">
				<select class="layui-input" name="parentIds" id="parentIds" lay-verify="required">
				</select>
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label">请求地址：</label>
			<div class="layui-input-inline" style="width: 350px">
				<input type="text" name="path" id="path" required lay-verify="required" placeholder="" autocomplete="off" class="layui-input">
			</div>
		</div>
		<div class="layui-form-item">
			<div class="layui-btn-container" style="text-align: center;">
				<button class="layui-btn" lay-submit lay-filter="formDemo">编辑</button>
			</div>
		</div>

		<script>
			//Demo
			layui.use(['form'], function() {
				var form = layui.form;
				var $ = layui.jquery;
				var pid = $("#parentId").val();
				//解决跨域
				$.ajaxPrefilter(function(options, originalOptions, jqXHR) {
					options.xhrFields = {
						withCredentials: true
					}
				})

				function getType() {
					$.ajax({
						url: globalData.server + "Modules/getAllParentId",
						data: {},
						success: function(data) {
							console.log(data);
							var select = document.getElementById("parentIds");
							if(data != null || data.size() > 0) {
								for(var c in data) {
									var option = document.createElement("option");
									option.setAttribute("value", data[c].id);
									option.innerText = data[c].name;
									if(data[c].id == pid) {
										option.setAttribute("selected", "true");
									}
									select.appendChild(option)
								}
							};
						}
					});
				}
				getType();
				//监听提交
				form.on('submit(formDemo)', function(data) {
					var d = {
						'id': $("#id").val(),
						'name': $("#name").val(),
						'parentId': $("#parentIds").val(),
						'path': $("#path").val(),
					}; //表单中的数据，定义一个对象来接收
					var url = globalData.server + './Modules/update';
					$.post(url, d, function(data) {
						console.log(data);
						if(data.count > 0) {
							var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
							parent.layer.close(index); //再执行关闭   
						} else {
							layer.msg(data.msg, {
								icon: 6
							});
						}
					}, "json");
					return false;
				});
			});
		</script>
	</body>

</html>